package com.atguigu.guli.service.oss.controller;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.oss.service.FileService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;


@RestController
@Slf4j
@RequestMapping("/admin/oss")
public class FileController {
    @Value("${server.port}")
    Integer port;
    @Autowired
    FileService fileService;
    @PostMapping("upload")
    public R upload(@RequestParam("file") MultipartFile multipartFile,@RequestParam("module") String module){//module 指的是文件的上传路径
        try {
            InputStream inputStream = multipartFile.getInputStream();
            String filename = multipartFile.getOriginalFilename();
            String uploadurl = fileService.upload(inputStream, module, filename);
            System.out.println(uploadurl);
            return R.ok().data("url",uploadurl).message("文件上传成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error();
    }
    @ApiOperation(value = "test测试接口")
    @GetMapping("test")
    public R test(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info(port+"端口的oss的test接口被调用");
        return R.ok();
    }
    @ApiOperation(value = "删除阿里云中的文件")
    @GetMapping("delete")
    public R delete(@RequestParam String url){
        fileService.deleteFromAliyun(url);
        return R.ok();
    }
}
